


//抽奖资格   已用完 与  未用完
var $lottery_btn = $('#web .lottery-btn'),
	$win_share   = $('#web .share-wrap');
	
//var   抽奖结果显示区域
var $success = $('#web .success'),
	$defeat = $('#web .defeat'),
	$success_text = $('#web .success .result div');
	

//再抽一次  点击事件绑定     :弹出提示分享可获得抽奖机会一次
$lottery_btn.on('click',function(){
	//抽奖
	$win_share.fadeIn();
});
$win_share.on('click',function(){
	$win_share.fadeOut();
});


//抽奖机会   true：再抽一次  ;   false:您的抽奖机会已用完
function lotterybtn(off){
	if(off){
		$lottery_btn.addClass('active');
		$lottery_btn.text('再抽一次');
	}else{
		$lottery_btn.removeClass('active');
		$lottery_btn.text('您的抽奖机会已用完');
		$lottery_btn.unbind('click');
	}
}

//分享成功  off   true:成功    false：未分享
var $sharenext = $('#web .lottery-sharenext');
function sharenext(off){
	if(off){
		$lottery_btn.css('display','none');
		$sharenext.css('display','block');
	}else{
		$lottery_btn.css('display','block');
		$sharenext.css('display','none');
	}
}


//抽奖结果动画
var selText = null,
	selResult = null;
function animated(sel){
	selText = $(sel+' .result-text');
	selResult = $(sel+' .result');
	selText.animate({
		top:'0px'
	},1000,function(){
		$(this).css('transform','rotate(0deg)');
		setTimeout(function(){
			selResult.fadeIn();
		},500);
	});
}






var userId = getUrlParam('id');
var tag =  null;
if(getUrlParam('tag')=='false'){
	tag = false;
}else{
	tag = getUrlParam('tag');
}
var prize = null,
	lottery_num = null,
	share_num   = null,
	draw = null;


downdata(tag);
function downdata(tag){
	$.ajax({
		type:"post",
		url:"http://m2.t1.laisj.com/flow/flowpackage/getPrize",
		async:false,
		data:{
			id:userId,
			draw:draw,
			tag:tag
		},
		success:function(res){    //成功得到返回时运行
			console.log(res);
	        if(res.code==200){
	        	prize = res.data.prize;
	        	jundgePrize(prize);
	        	
	        	lottery_num = res.lucky_times;
	        	share_num  = res.share_times;
	        	if(lottery_num==0&&share_num>0){
	        		//抽奖资格已用完
		        	sharenext(false);
		        	lotterybtn(false);
	        	}else{
	        		sharenext(false);
	        		lotterybtn(true);
	        		if(lottery_num>0&&share_num>1){
	        			draw = 1;
		        		tag = false;
		        		sharenext(true);
	        		}
	        	}
	        }
	        if(res.code==400){
	        	//未参与
	        	location.href = 'index.html';
	        }
	    },
	    ajaxError:function(a,b,c){
	        console.log(a);      //错误的对象
	        console.log(b);      //错误的类型
	        console.log(c);      //错误的信息
	    },
	    dataType:'JSON'
	});
}



//判断获得几等奖    0:未中奖, 1:一等奖, 2:二等奖, 3:三等奖
function jundgePrize(prize){
	toggleView(false);
	switch (true){
		case prize==0:
			toggleView(false);
			break;
		case prize==1:
			toggleView(true);
			$success_text.text('一等奖');
			break;
		case prize==2:
			toggleView(true);
			$success_text.text('二等奖');
			break;
		case prize==3:
			toggleView(true);
			$success_text.text('三等奖');
			break;	
	}
}
//未中奖   off= false ;   中奖   off = true
function toggleView(off){
	if(off){
		$success.css('display','block');
		$defeat.css('display','none');
		animated('.success ');
	}else{
		$success.css('display','none');
		$defeat.css('display','block');
		animated('.defeat ');
	}
}








//微信分享
var title = '来设计免费帮你打造爆款';
wx.ready(function(){
	wx.onMenuShareTimeline({
	    title: title,   // 分享标题
	    link: 'http://m2.t1.laisj.com/flow/flowpackage/jumptoIntention',     // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
	    imgUrl: 'http://x1.t1.laisj.com/H5/lotteryH5/img/shareimg.png', // 分享图标
	    success: function () { 
	        // 用户确认分享后执行的回调函数
	        console.log('successShareTimeline');
	        shareajax();
	        _hmt.push(['_trackEvent', 'sharelottery', 'ShareTimeline', 'resultpage',0]);
	    },
	    cancel: function () { 
	        // 用户取消分享后执行的回调函数
	        _hmt.push(['_trackEvent', 'sharelottery', 'ShareTimeline', 'resultpage',2]);
	    }
	});
	//分享给朋友
	wx.onMenuShareAppMessage({
	    title: title, // 分享标题
	    desc: '我们拥有打造爆款产品的一切先发条件！', // 分享描述
	    link: 'http://m2.t1.laisj.com/flow/flowpackage/jumptoIntention', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
	    imgUrl: 'http://x1.t1.laisj.com/H5/lotteryH5/img/shareimg.png', // 分享图标
	    type: 'link', // 分享类型,music、video或link，不填默认为link
	    dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
	    success: function () { 
	        // 用户确认分享后执行的回调函数
	        console.log('successShareAppMessage');
	        shareajax();
	        _hmt.push(['_trackEvent', 'sharelottery', 'ShareAppMessage', 'resultpage',0]);
	    },
	    cancel: function () { 
	        // 用户取消分享后执行的回调函数
	        _hmt.push(['_trackEvent', 'sharelottery', 'ShareAppMessage', 'resultpage',2]);
	    }
	});
});
wx.error(function(res){
   console.log(res); 
});



//分享获取抽奖机会
function shareajax(){
	$win_share.trigger('click');
	$.ajax({
		type:"post",
		url:"http://m2.t1.laisj.com/flow/flowpackage/share",
		async:false,
		data:{
			id:userId
		},
		success:function(res){    //成功得到返回时运行
			console.log(res);
	        if(res.code==200){
	        	if(lottery_num==0&&share_num>0){
	        		
	        	}else{
	        		draw = 1;
	        		tag = false;
	        		sharenext(true);
	        	}
	        }
	    },
	    ajaxError:function(a,b,c){
	        console.log(a);      //错误的对象
	        console.log(b);      //错误的类型
	        console.log(c);      //错误的信息
	    },
	    dataType:'JSON'
	});
}



//分享成功再抽一次   按钮点击事件
var topheight = $('.success .result-text').css('top');
$sharenext.on('click',function(){
	sharenext(false);
	
	//将抽奖动画初始化
	changeCss('.defeat ');
	changeCss('.success ');
	
	//抽奖
	tag = false;
	downdata(tag);
});

//将抽奖动画初始化
function changeCss(sel){
	selText = $(sel+' .result-text');
	selResult = $(sel+' .result');
	selText.css('top',topheight);
	selText.css('transform','rotate(-15deg)');
	selResult.css('display','none');
}




